package com.oneproject.infrastructure.data;

import com.oneproject.domain.Entity;
import com.oneproject.domain.Repository;
import com.oneproject.domain.UnitOfWork;

import java.util.Collection;

/**
 * 仓储的通常实现类
 *
 * @author 马文韬
 * @date 2018/9/7
 * @version 1.0
 */
public class BaseRepository<E extends Entity> implements Repository<E> {

    private UnitOfWork unitOfWork;

    public BaseRepository(UnitOfWork unitOfWork) {
        this.unitOfWork = unitOfWork;
    }

    public UnitOfWork getUnitOfWork() {
        return this.unitOfWork;
    }

    public E get(Object id, Class<E> entityClazz) {
        return this.unitOfWork.selectByPrimaryKey(id, entityClazz);
    }

    public Collection<E> getAll(Class<E> entityClazz) {
        return this.unitOfWork.selectAll(entityClazz);
    }

    public void insert(E item) {
        this.unitOfWork.insert(item);
    }

    public void insert(Collection<E> items) {
        for (E item:
             items) {
            insert(item);
        }
    }

    public void delete(E item) {
        this.unitOfWork.delete(item);
    }

    public void delete(Collection<E> items) {
        for (E item:
                items) {
            delete(item);
        }
    }

    public void update(E item) {
        this.unitOfWork.update(item);
    }

    public void update(Collection<E> items) {
        for (E item:
                items) {
            update(item);
        }
    }
}
